package com.zdl.interestingapp.utils

import org.jetbrains.annotations.NotNull
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*

/**
 * @author zdl
 * @date 2018/4/25 11:18
 * email zdl328465042@163.com
 * description 封装日期相关工具类
 */
object MyDateUtil {
    private const val dateFormat = "yyyy-MM-dd HH-mm-ss"
    private val sdf = SimpleDateFormat(dateFormat, Locale.getDefault())

    /**
     * 获取系统时间 格式为:yyyy-MM-dd HH-mm-ss
     * @return date
     */
    fun getCurrentDate(): String {
        return sdf.format(getCurrentTime())
    }

    /**
     * 获取系统时间戳
     * @return time
     */
    fun getCurrentTime(): Long {
        return System.currentTimeMillis()
    }

    /**
     * 时间戳转换成字符串
     * @param time time
     * @return date
     */
    fun time2date(@NotNull time: Long): String {
        val date = Date(time)
        return sdf.format(date)
    }

    /**
     * 字符串转换为时间戳，需要保持字符串格式一样
     * @param dateStr dateStr
     * @return time
     */
    fun date2time(@NotNull dateStr: String): Long {
        var date = Date()
        try {
            date = sdf.parse(dateStr)
        } catch (e: ParseException){
            e.printStackTrace()
        }
        return date.time
    }
}